#region Using directives
using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using WP.MF.DataEntity;
using WP.MF.BusinessRule;
using WP.MF.DataAccess;
using WP.DB;
using WP.Commons;
#endregion

namespace WP.MF.BusinessLogical
{

    /// <summary>
    /// Created at 8/2/2006 by dan
    /// </summary>
    /// 

    public class MCurrencyBLL : IBusinessLogical
    {
        MCurrencyDAL mCurrencyDAL = new MCurrencyDAL();

        #region public MCurrencyBLL()
        public MCurrencyBLL()
        {
        }
        #endregion

        #region public MCurrencyDAL SelectCurrencyByRecID()
        public MCurrencyDEL SelectCurrencyByRecID(DBAccess dBAccess, string strRecID)
        {
            MCurrencyDEL mCurrencyDEL = new MCurrencyDEL();

            DataFunctions.SelectByRecID(dBAccess, mCurrencyDEL, mCurrencyDAL.Fields, strRecID);

            return mCurrencyDEL;
        }


        public MCurrencyDEL SelectCurrencyByRecID(string strRecID)
        {
            DBAccess dBAccess = new DBAccess(false, false);

            MCurrencyDEL mCurrencyDEL = null;

            try
            {
                mCurrencyDEL = SelectCurrencyByRecID(dBAccess, strRecID);
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
            }
            finally
            {
                dBAccess.CloseCon();
            }
            return mCurrencyDEL;
        }
        #endregion

        #region Select Record by RECID
        public IDataEntity SelectDataByRecID(string strRecID)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            try
            {
                MCurrencyDEL mCurrencyDEL = SelectCurrencyByRecID(dBAccess, strRecID);
                return mCurrencyDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

        #region public bool InsertCurrency()
        public bool Insert(IDataEntity iMCurrencyDEL)
        {
            MCurrencyDEL mCurrencyDEL = iMCurrencyDEL as MCurrencyDEL;
            DBAccess dBAccess = new DBAccess(false, true);
            MCurrencyBRL mCurrencyBRL = new MCurrencyBRL();

            try
            {
                if (mCurrencyBRL.InsertValid(dBAccess, mCurrencyDEL))
                {
                    //Insert Into 
                    
                    DataFunctions.InsertTable(dBAccess, mCurrencyDAL.Fields, mCurrencyDEL);
                    mCurrencyDAL.InsertMCurrencyHist(dBAccess, mCurrencyDEL);

                    dBAccess.CommitTranAndCloseCon();
                    return true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                    return false;
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }
        #endregion

        #region public bool DeleteCurrency()
        public bool Delete(string strRecId, byte[] btOldRecStamp)
        {
            MCurrencyBRL mCurrencyBRL = new MCurrencyBRL();
            DBAccess dBAccess = new DBAccess(false, true);
            bool bFlag = false;

            try
            {
                if (mCurrencyBRL.DeleteValid(dBAccess, strRecId, btOldRecStamp))
                {
                    mCurrencyDAL.DeleteMCurrency(dBAccess, strRecId);

                    dBAccess.CommitTranAndCloseCon();

                    bFlag = true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                    bFlag = false;
                }


            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                bFlag = false;
            }

            return bFlag;
        }

        #endregion

        #region public bool UpdateCurrency()
        public bool Update(IDataEntity iMCurrencyDEL)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            MCurrencyBRL mCurrencyBRL = new MCurrencyBRL();
            MCurrencyDEL mCurrencyDEL = iMCurrencyDEL as MCurrencyDEL;
            try
            {
                if (mCurrencyBRL.UpdateValid(dBAccess, mCurrencyDEL))
                {
                    DataFunctions.UpdateTable(dBAccess, mCurrencyDAL.Fields,MCurrencyDEL.TABLENAME_MCURRENCY , mCurrencyDEL.Tables[0].Rows[0]  );
                    mCurrencyDAL.InsertMCurrencyHist(dBAccess, mCurrencyDEL);
                    dBAccess.CommitTranAndCloseCon();
                    return true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                    return false;
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }
        #endregion

    }
}